package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import java.util.List;
import javax.swing.ImageIcon;
import logica.Verificador;
import logica.VerificadorException;

/**
 *
 * @author Mariano Zapata
 */
public class DialogoConfiguracion extends javax.swing.JPanel {

    private boolean retorno;
    private PanelConfiguracionRed pcr;
    private PanelConfiguracionAplicaciones pca;
    
    /** Creates new form DialogoConfiguracion */
    public DialogoConfiguracion(List<String []> aplicaciones, String ipServidor, String puertoRMIServidor,
            String puertoRMILocal, List<String> tarjetasRed, String interfaceConfigurada, int tipoIP)
    {
        initComponents();
        Image icono= new ImageIcon(getClass().getResource("/gui/icono/logo_chico.png")).getImage();
        dialogo.setIconImage(icono);
        dialogo.getContentPane().setLayout(new BorderLayout());
        pcr= new PanelConfiguracionRed(ipServidor, puertoRMIServidor, puertoRMILocal, tarjetasRed,
                interfaceConfigurada, tipoIP);
        pca= new PanelConfiguracionAplicaciones(aplicaciones);
        pestanias.addTab("Parámetros de red", pcr);
        pestanias.addTab("Aplicaciones externas", pca);
        pestanias.setSelectedIndex(0);
        retorno= false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialogo = new javax.swing.JDialog();
        pestanias = new javax.swing.JTabbedPane();
        guardar = new javax.swing.JButton();

        dialogo.setTitle("Configuración");
        dialogo.setModal(true);
        dialogo.setResizable(false);

        javax.swing.GroupLayout dialogoLayout = new javax.swing.GroupLayout(dialogo.getContentPane());
        dialogo.getContentPane().setLayout(dialogoLayout);
        dialogoLayout.setHorizontalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        dialogoLayout.setVerticalGroup(
            dialogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        guardar.setText("Guardar");
        guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardar(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pestanias, javax.swing.GroupLayout.DEFAULT_SIZE, 704, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(guardar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pestanias, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(guardar)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void guardar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardar
        try {
            pcr.quitarErrores();
            Verificador.verificarPuertos(pcr.getPuertoRMIServidor(), pcr.getPuertoRMILocal());
            retorno= true;
            dialogo.setVisible(false);
        } catch (VerificadorException ex) {pestanias.setSelectedIndex(0);
                                           pcr.marcarErrores(ex.obtenerErrores());}
    }//GEN-LAST:event_guardar

    public boolean hacerVisible(Component c)
    {
        dialogo.getContentPane().add(this, BorderLayout.CENTER);
        dialogo.pack();
        dialogo.setLocationRelativeTo(c);
        dialogo.setVisible(true);
        dialogo.dispose();
        return retorno;
    }
    
    public String getIpServidor() {
        return pcr.getIpServidor();
    }
    
    public String getPuertoRMIServidor() {
        return pcr.getPuertoRMIServidor();
    }
    
    public String getPuertoRMILocal() {
        return pcr.getPuertoRMILocal();
    }
    
    public String getInterfaceSeleccionada() {
        return pcr.getInterfaceRedSeleccionada();
    }
    
    public int getTipoIP() {
        return pcr.getTipoIP();
    }
    
    public List<String []> getAplicaciones() {
        return pca.getAplicaciones();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog dialogo;
    private javax.swing.JButton guardar;
    private javax.swing.JTabbedPane pestanias;
    // End of variables declaration//GEN-END:variables
}
